// pages/cart/cart.js
var core = require("../../utils/core.js");
import Toast from '../../components/dist/toast/toast';
import Notify from '../../components/dist/notify/notify';
import Dialog from '../../components/dist/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sum_price: '',
    sum_total: '',
    list: [],
    allChoosed: false, //全选状态
    loadingShow: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getCartData();
  },
  getCartData: function() {
    var that = this;
    that.setData({
      loadingShow: true
    })
    core.get("groupapp/goods/cart_statistics", { //获取购物车信息
    }, function(t) {
      if (t.error == 0) {
        that.setData({
          loadingShow: false,
          list: t.list,
          sum_price: t.sum_price,
          sum_total: t.sum_total,
        })
        that.isAllSelected();
      } else {}
    })
  },
  goIndex: function() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  isAllSelected: function() { //判断是否全部选中
    var that = this;
    var data = this.data.list;
    var flag = true; //默认全部选中
    if (data.length==0){
      that.setData({
        allChoosed: false
      })
    }else{
      for (var i = 0; i < data.length; i++) {
        if (data[i].selected == 0) { //有没选中的
          flag = false
        }
      }
      that.setData({
        allChoosed: flag
      })
    }
  },
  onChange: function(e) { //全选
    var that = this;
    var allSelectStatus = !this.data.allChoosed; //全选改变后的状态
    var data = this.data.list;
    var idStr = '';
    for (var i = 0; i < data.length; i++) {
      if (i == data.length - 1) {
        idStr += data[i].id
      } else {
        idStr += data[i].id + ','
      }
    }
    this.setData({
      allChoosed: !this.data.allChoosed
    })
    that.setData({
      loadingShow: true
    })
    core.get("groupapp/goods/cart_select", { //上传修改信息
      id: idStr,
      selected: allSelectStatus ? '1' : '0'
    }, function(t) {
      if (t.error == 0) {
        that.setData({
          loadingShow: false,
          list: t.list,
          sum_price: t.sum_price,
          sum_total: t.sum_total
        })
      } else {
        console.log('error')
      }
    })
  },
  jianhao: function(e) { //点击减号
    var that = this;
    var data = this.data.list
    var index = e.target.dataset.index;
    var id = e.target.dataset.id;
    if (data[index].total == 1) {
      Toast('最少购买' + 1 + '件');
      return
    } else {
      data[index].total = parseInt(data[index].total) - 1
    }
    that.setData({
      loadingShow: true
    })
    core.get("groupapp/goods/update_cart", { //上传修改信息
      id: id,
      total: data[index].total
    }, function(t) {

      if (t.error == 0) {
        that.setData({
          loadingShow: false,
          list: data,
          sum_price: t.sum_price,
          sum_total: t.sum_total
        })
      }
    })
  },
  jiahao: function(e) { //点击加号
    var that = this;
    var data = this.data.list
    var index = e.target.dataset.index;
    var id = e.target.dataset.id;
    var maxNum = parseInt(e.target.dataset.maxcount);
    if (data[index].total == maxNum) {
      Toast('最多购买' + maxNum + '件');
      return
    } else {
      data[index].total = parseInt(data[index].total) + 1
    }
    that.setData({
      loadingShow: true
    })
    core.get("groupapp/goods/update_cart", { //上传修改信息
      id: id,
      total: data[index].total
    }, function(t) {
      if (t.error == 0) {
        that.setData({
          loadingShow: false,
          list: data,
          sum_price: t.sum_price,
          sum_total: t.sum_total
        })
      }
    })
  },
  onCartItemChange: function(e) { //点击购物车单选框
    var that = this;
    var data = this.data.list;
    var id = e.target.dataset.id;
    var index = e.currentTarget.dataset.index;
    var type = ''; //记录变更状态
    if (data[index].selected == 1) {
      data[index].selected = 0;
      type = 0;
    } else if (data[index].selected == 0) {
      data[index].selected = 1;
      type = 1;
    }
    that.setData({
      loadingShow: true
    })
    core.get("groupapp/goods/cart_select", { //上传修改信息
      id: id,
      selected: type
    }, function(t) {
      if (t.error == 0) {
        that.setData({
          loadingShow: false,
          list: data,
          sum_price: t.sum_price,
          sum_total: t.sum_total
        })
        that.isAllSelected();
      } else {
        console.log('error')
      }
    })
  },
  onDeleteClose(event) {
    var that = this;
    console.log(event);
    var id = event.target.dataset.id;
    const {
      position,
      instance
    } = event.detail;
    switch (position) {
      case 'left':
      case 'cell':
        instance.close();
        break;
      case 'right':
        Dialog.confirm({
          message: '确定删除吗？'
        }).then(() => {
          console.log('确认删除')
          that.setData({
            loadingShow: true
          })
          core.get("groupapp/goods/delect_cart", { //上传修改信息
            id: id,
          }, function(t) {
            if (t.error == 0) {            
              Notify({
                text: '删除成功',
                duration: 1000,
                selector: '#custom-selector',
                backgroundColor: '#1989fa'
              });
              that.getCartData();
            } else {
              console.log('error')
            }
            that.setData({
              loadingShow: false
            })
          })
        }).catch(() => {
          instance.close();
          console.log('取消')
        });
        break;
    }
  },
  gotoSettlement:function(){//去结算
    var that = this;
      wx.setStorage({
        key: 'goodsInfo',
        data: that.data.list,
        success:function(){
              wx.navigateTo({
                url: '/pages/confirmOrder/confirmOrder?type=cart',
              })
        }
      })


  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})